package com.anhry.app.safety.manage.enterprise.struts.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;

import jxl.Sheet;
import jxl.Workbook;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.anhry.app.safety.manage.enterprise.bean.TbCorpEconomyType;
import com.anhry.app.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.app.safety.manage.enterprise.bean.TbCorpPerson;
import com.anhry.app.safety.manage.enterprise.bean.TbCorpType;
import com.anhry.app.safety.manage.enterprise.struts.base.EnterpriseBaseAction;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.manage.system.bean.TbUserGrade;
import com.anhry.app.safety.manage.system.bean.TbUserRole;
import com.anhry.app.safety.util.Constants;
import com.anhry.app.safety.util.Environment;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.PwdDigest;

/**
 * @date:Jun 19, 2012
 * @remark:
 * @author:dongbin
 */
@Controller
@Scope("prototype")
public class ExcelExportAction extends EnterpriseBaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Environment env = Environment.getInstance();
	private File dbFile;
	private String dbFileContentType;
	private String dbFileFileName;
	
	public String loadExport(){
		return "load_export";
	}
	
	/**
	 * 导入企业国民经济类型（20大类）
	 * @return
	 */
	public String economySave(){
		try {
			if(this.dbFile == null || !this.dbFile.exists()){
				this.setTip("附件不能为空！");
				return "load_input";
			}
			//进入导入程序 
			long start = new Date().getTime();
			File file = this.dbFile;
			InputStream is = new FileInputStream(file.getPath());
            Workbook rwb = Workbook.getWorkbook(is);	//声名一个工作薄
            Sheet st = rwb.getSheet(2);	//第0个工作表
            int Rows=st.getRows();
            int Cols=st.getColumns();
            Loggers.info("Excel数据导入：当前文件名："+this.dbFileFileName+" 当前工作表的名字："+st.getName()+" 总行数:"+Rows+"总列数:"+Cols);
            
            int count = 0;
            for(int c=0;c<Cols;c++){
            	if(null!=st.getCell(c, 0).getContents() && !"".equals(st.getCell(c, 0).getContents())){
            		count+=1;
            	}
            }
            TbCorpEconomyType obj = null;
            for(int i=1;i<Rows;i++){
              	if(null!=st.getCell(0,i).getContents() && st.getCell(0, i).getContents().length()>0){
              		obj = new TbCorpEconomyType();
              		obj.setIsDele("0");
              		obj.setTypeCode(st.getCell(2, i).getContents());
              		obj.setTypeName(st.getCell(1, i).getContents());
              		obj.setTypeParentId(Long.parseLong("0"));
              		obj.setTypeRemark("");
              		this.utilService.save(obj);
              	}
              	if(null!=st.getCell(3,i).getContents() && st.getCell(3, i).getContents().length()>0){
              		TbCorpEconomyType obj1 = new TbCorpEconomyType();
              		obj1.setIsDele("0");
              		obj1.setTypeCode(st.getCell(4, i).getContents());
              		obj1.setTypeName(st.getCell(3, i).getContents());
              		obj1.setTypeParentId(obj.getId());
              		obj1.setTypeParentName(obj.getTypeName());
              		obj1.setTypeRemark(st.getCell(5, i).getContents());
              		this.utilService.save(obj1);
              	}
              	
              	System.out.println("export==="+(i+1)+"行");
            }
            rwb.close();
            long end = new Date().getTime();
            Loggers.info(st.getName()+"导入成功！所用的时间:"+(end-start)/1000+"秒");
            this.setTip("导入成功！所用时间："+(end-start)/1000+"秒");
            return SUCCESS;
		} catch (Exception e) {
			Loggers.error("enterprise模块:ExcelExportAction:economySave()方法出错："+e);
			e.printStackTrace();
		}
		return ERROR;
	}
	
	/**
	 * 导入部门
	 * @return
	 */
	public String deptSave(){
		try {
			if(this.dbFile == null || !this.dbFile.exists()){
				this.setTip("附件不能为空！");
				return "load_input";
			}
			//进入导入程序 
			long start = new Date().getTime();
			File file = this.dbFile;
			InputStream is = new FileInputStream(file.getPath());
			Workbook rwb = Workbook.getWorkbook(is);	//声名一个工作薄
			Sheet st = rwb.getSheet(0);	//第0个工作表
			int Rows=st.getRows();
			int Cols=st.getColumns();
			Loggers.info("Excel数据导入：当前文件名："+this.dbFileFileName+" 当前工作表的名字："+st.getName()+" 总行数:"+Rows+"总列数:"+Cols);
			
			int count = 0;
			for(int c=0;c<Cols;c++){
				if(null!=st.getCell(c, 0).getContents() && !"".equals(st.getCell(c, 0).getContents())){
					count+=1;
				}
			}
			TbUserDept obj1 = null;
			TbUserDept obj2 = null;
			TbUserDept obj3 = null;
			for(int i=2;i<Rows;i++){
				if(null!=st.getCell(0,i).getContents() && st.getCell(0, i).getContents().length()>0){
					obj1 = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, Long.parseLong(st.getCell(0, i).getContents()));
				}
				if(null!=st.getCell(2,i).getContents() && st.getCell(2, i).getContents().length()>0){
					obj2 = new TbUserDept();
					obj2.setDeptClass("1");
					obj2.setDeptCode(st.getCell(2, i).getContents());
					obj2.setDeptName(st.getCell(3, i).getContents());
					obj2.setDeptParentId(obj1.getId());
					obj2.setDeptParentName(obj1.getDeptName());
					obj2.setDeptType("3");
					obj2.setDeptZg("0");
					obj2.setIsDele("0");
					this.utilService.save(obj2);
				}
				if(null!=st.getCell(4,i).getContents() && st.getCell(4, i).getContents().length()>0){
					obj3 = new TbUserDept();
					obj3.setDeptClass("3");
					obj3.setDeptCode(st.getCell(4, i).getContents());
					obj3.setDeptName(st.getCell(5, i).getContents());
					obj3.setDeptParentId(obj2.getId());
					obj3.setDeptParentName(obj2.getDeptName());
					obj3.setDeptType("3");
					obj3.setDeptZg("0");
					obj3.setIsDele("0");
					this.utilService.save(obj3);
				}
				
				System.out.println("export==="+(i+1)+"行");
			}
			rwb.close();
			long end = new Date().getTime();
			Loggers.info(st.getName()+"导入成功！所用的时间:"+(end-start)/1000+"秒");
			this.setTip("导入成功！所用时间："+(end-start)/1000+"秒");
			return SUCCESS;
		} catch (Exception e) {
			Loggers.error("enterprise模块:ExcelExportAction:economySave()方法出错："+e);
			e.printStackTrace();
		}
		return ERROR;
	}
	
	private String getContent(Sheet st, int col, int row) {
		String content = st.getCell(col, row).getContents();
		if(content != null) {
			content = content.trim();
			if("不填".equals(content)) {
				return null;
			} else if("是".equals(content) || "有".equals(content)){
				return "1";
			} else if("否".equals(content) || "无".equals(content)) {
				return "0";
			}
			if(content.length() > 0) {
				return content;
			}
		} 
		return null;
	}
	/**
	 * 导入企业
	 * @return
	 */
	public String corpSave(){
		try {
			if(this.dbFile == null || !this.dbFile.exists()){
				this.setTip("附件不能为空！");
				return "load_input";
			}
			//进入导入程序 
			long start = new Date().getTime();
			File file = this.dbFile;
			InputStream is = new FileInputStream(file.getPath());
			Workbook rwb = Workbook.getWorkbook(is);	//声名一个工作薄
			Sheet st = rwb.getSheet(2);	//第0个工作表
			int Rows=st.getRows();
			int Cols=st.getColumns();
			Loggers.info("Excel数据导入：当前文件名："+this.dbFileFileName+" 当前工作表的名字："+st.getName()+" 总行数:"+Rows+"总列数:"+Cols);
			
	/*		int count = 0;
			for(int c=0;c<Cols;c++){
				if(null!=st.getCell(c, 0).getContents() && !"".equals(st.getCell(c, 0).getContents())){
					count+=1;
				}
			}*/
			TbCorpInfo obj = null;
			String county = "";
			String unit = "";
			String dept = "";
			for(int i=1;i<Rows;i++){
				System.out.println( st.getCell(1, i).getContents());
				if(null!=st.getCell(1,i).getContents() && st.getCell(1, i).getContents().length()>0){
					obj = new TbCorpInfo();
					String corpName = st.getCell(1, i).getContents();
					if(corpName != null) {
						obj.setCorpName(corpName.trim());
						obj.setCorpGsCode(getContent(st, 2, i));
						obj.setCorpOrgCode(getContent(st, 3, i));
						obj.setCorpRegAddress(getContent(st,4,i));
						obj.setCorpWorkAddress(getContent(st,5,i));
						obj.setCorpOfficeAddress(getContent(st,6,i));
						obj.setCorpPost(getContent(st,7,i));
						obj.setCorpPhone(getContent(st,8,i));
						obj.setCorpMobilePhone(getContent(st,9,i));
						obj.setCorpEmail(getContent(st,10,i));
						String type1 = getContent(st,11,i);
						if(null!=type1 && type1.length()>0){
							TbCorpType type = this.getTypeByName(type1.trim());
							if(null!=type){
								obj.setTbCorpType(type);
							}
						}
						String type2 =getContent(st,12,i);
						if(null!=type2 && type2.length()>0){
							TbCorpType type = this.getTypeByName(type2.trim());
							if(null!=type){
								obj.setTbCorpTypeMin(type);
							}
						}
						obj.setCorpProvince(221l);
						obj.setCorpCity(291l);
						county =getContent(st,13,i);
						if(null!=county && county.length()>0){
							TbUserDept d1 = this.getDeptByName(county.trim());
							if(null!=d1){
								obj.setTbCounty(d1);
							}
						}
						unit = getContent(st,14,i);
						if(null!=unit && unit.length()>0){
							TbUserDept d1 = this.getDeptByName(unit.trim());
							if(null!=d1){
								obj.setTbStreet(d1);
							}
						}
						dept = getContent(st,15,i);
						if(null!=dept && dept.length()>0){
							TbUserDept d1 = this.getDeptByName(dept.trim());
							if(null!=d1){
								obj.setTbVillage(d1);
							}
						}
						String zichan = getContent(st,16,i);
						if(zichan != null) {
							zichan = zichan.trim().replaceAll("万", "");
							obj.setCorpGudingzichang(Double.parseDouble(zichan));
						}
						String capital = getContent(st,17,i);
						if(capital != null) {
							capital = capital.trim().replaceAll("万", "");
							obj.setCorpGudingzichang(Double.parseDouble(capital));
						}
						String date = getContent(st,18,i);
						if(date != null) {
							obj.setCorpRegDate(formatDate(date));
						}
						//corp area
						
						String area1 =  getContent(st,20,i);
						String area2 =  getContent(st,21,i);
						if(area1 != null) {
							try  {
								obj.setCorpDishang(Double.parseDouble(area1));
							}catch (Exception e) {
								System.out
										.println(e.getMessage());
							}
						}
						if(area2 != null) {
							try  {
								obj.setCorpDishang(Double.parseDouble(area2));
							}catch (Exception e) {
								System.out
										.println(e.getMessage());
							}
						}
						
						
						if(null!= getContent(st,23,i) && getContent(st,23,i).length()>0){
							obj.setCorpCyrs(Integer.parseInt(getContent(st,23,i)));
						}
						if(null!=getContent(st,24,i) && getContent(st,24,i).length()>0){
							obj.setCorpAqrs(Integer.parseInt(getContent(st,24,i)));
						}
						obj.setCorpXkCode(getContent(st, 25, i));
						//安全许可证
						date= getContent(st,26,i);
						if(date != null) {
							obj.setCorpXkStart(formatDate(date));
						}
						date= getContent(st,27,i);
						if(date != null) {
							obj.setCorpXkEnd(formatDate(date));
						}
						//危化许可证
						obj.setCorpWhxkCode(getContent(st, 28, i));
						date= getContent(st,29,i);
						if(date != null) {
							obj.setCorpWhxkStart(formatDate(date));
						}
						date= getContent(st,30,i);
						if(date != null) {
							obj.setCorpWhxkEnd(formatDate(date));
						}
						
						obj.setNationGrade(getContent(st,32,i));
						String reportType = getContent(st, 33, i);
						if(reportType != null) {
							if("半月报".equals(reportType)) {
								obj.setReportType("0");
							} else {
								obj.setReportType("1");
							}
						}
						obj.setCorpJingyixiangmu(getContent(st,34,i));
						obj.setCorpYuanliao(getContent(st,35,i));
						obj.setCorpChanpin(getContent(st,36,i));
					
						String economicType = getContent(st, 37, i);
						if(economicType != null) {
							if("股份有限".equals(economicType)) {
								obj.setCorpProperty("16");
							} else if("国有股份".equals(economicType)) {
								obj.setCorpProperty("11");
							}else if("个人独资".equals(economicType) || "个体".equals(economicType) || 
									"私营独资".equals(economicType) || "个体经营".equals(economicType)) {
								obj.setCorpProperty("17");
							}else if("有限责任公司".equals(economicType)) {
								obj.setCorpProperty("15");
							}else if("集体所有制".equals(economicType)) {
								obj.setCorpProperty("12");
							}else  {
								obj.setCorpProperty("90");
							}
						}
						//System.out.println("is zyb " + getContent(st, 49, i));
						obj.setCorpIsZyb(getContent(st, 47, i));
						obj.setCorpIsTeshe(getContent(st, 48, i));
						obj.setCorpIsShewei(getContent(st, 49, i));
						obj.setCorpIsBlzh(getContent(st, 50, i));
						obj.setIsBlastingDemolition(getContent(st, 51, i));
						obj.setCorpIsDanger(getContent(st, 52, i));
						if("1".equals(getContent(st, 53, i))) {
							obj.setCorpSize("2");
							obj.setCorpSjName(getContent(st, 54, i));
							if(obj.getCorpSjName() != null) {
								TbCorpInfo p = getCorpByName(obj.getCorpSjName());
								if(p != null) {
									obj.setCorpSjId(p.getId());
								}
							}
						} else  {
							obj.setCorpSize("1");
						}
						obj.setCorpStatus(getContent(st, 55, i));
						obj.setDistrictMonitor(getContent(st, 56, i));
						if("1".equals(getContent(st, 57, i))) {
							obj.setCorpLixu("40");
						}
						obj.setIsDele("0");
						obj.setCorpPwd(PwdDigest.passwordDigest("666666"));
						obj.setCorpPwd2("666666");
						obj.setCorpLoginCount(0);
						obj.setReportType("0");
						obj.setCorpRegType(1l);
						obj.setCorpAddTime(new Date());
						obj.setCorpReportType("1");
						obj.setCorpAddUser("system");
						this.utilService.save(obj);
						
						TbCorpPerson fr = new TbCorpPerson();
						fr.setCorpId(obj.getId());
						fr.setPerName(getContent(st, 40, i));
						fr.setPerPhone(getContent(st, 41, i));//电话
						fr.setPerMobile(getContent(st, 42, i));
						fr.setPerType("1");
						this.utilService.save(fr);
						
						TbCorpPerson fzr = new TbCorpPerson();
						fzr.setCorpId(obj.getId());
						fzr.setPerName(getContent(st, 43, i));
						fzr.setPerPhone(getContent(st, 44, i));//电话
						fzr.setPerMobile(getContent(st, 45, i));
						fzr.setPerType("2");
						this.utilService.save(fzr);
						
						TbCorpPerson aqfzr = new TbCorpPerson();
						aqfzr.setCorpId(obj.getId());
						aqfzr.setPerName(getContent(st, 46, i));
						aqfzr.setPerMobile(getContent(st, 47, i));
						aqfzr.setPerPhone(getContent(st, 48, i));//电话
						aqfzr.setPerType("3");
						this.utilService.save(aqfzr);
						//10,11,12 法人
						//22,23,24主要负责人
						
						//25,26,27安全负责人
						System.out.println("export==="+(i+1)+"行");
					}
				}
			}
			rwb.close();
			long end = new Date().getTime();
			Loggers.info(st.getName()+"导入成功！所用的时间:"+(end-start)/1000+"秒");
			this.setTip("导入成功！所用时间："+(end-start)/1000+"秒");
			return SUCCESS;
		} catch (Exception e) {
			Loggers.error("enterprise模块:ExcelExportAction:economySave()方法出错："+e);
			e.printStackTrace();
		}
		return ERROR;
	}
	
	/**
	 *  从文件夹进行导入
	 * @return
	 */
	public String updateCorpInfo(){
		try {
			long start = new Date().getTime();
			String source = env.getProperty("corp.import.source.folder");
			if(source != null && source.length() > 0) {
				File folder = new File(source);
				if(folder.isDirectory()) {
					File[] files = folder.listFiles(); 
					for(File xmlfiles:files) {
						if(xmlfiles.getName().endsWith(".xls")) {
							InputStream is = new FileInputStream(xmlfiles.getPath());
							Workbook rwb = Workbook.getWorkbook(is);	//声名一个工作薄
							Sheet st = rwb.getSheet(2);	//第0个工作表
							int Rows=st.getRows();
							int Cols=st.getColumns();
							//Loggers.info("Excel数据导入：当前文件名："+xmlfiles.getName()+" 当前工作表的名字："+st.getName()+" 总行数:"+Rows+"总列数:"+Cols);
							
							int count = 0;
							for(int c=0;c<Cols;c++){
								if(null!=st.getCell(c, 0).getContents() && !"".equals(st.getCell(c, 0).getContents())){
									count+=1;
								}
							}
							TbCorpInfo obj = null;
		
							for(int i=1;i<Rows;i++){
								if(null!=st.getCell(0,i).getContents() && st.getCell(0, i).getContents().length()>0){
									String corpName = st.getCell(1, i).getContents();
									System.out.println(corpName);
									if(corpName != null && !"".equals(corpName.trim())) {
										obj = getCorpByName(corpName.trim());
										
										if(obj != null) {
											String zichan = getContent(st,18,i);
											if(zichan != null) {
												zichan = zichan.trim().replaceAll("万", "");
												obj.setCorpGudingzichang(Double.parseDouble(zichan));
											}
											String capital = getContent(st,19,i);
											if(capital != null) {
												capital = capital.trim().replaceAll("万", "");
												obj.setCorpZhucezichang(Double.parseDouble(capital));
											}
											
											this.utilService.update(obj);
										
										System.out.println("export==="+(i+1)+"行");
										}
									}
								}
								
							}
							rwb.close();
							is.close();
						}
						if(xmlfiles.delete()) {
							System.out.println("Deleted file " + xmlfiles.getName());
						}
					}
				}
				
			} else {
				this.setTip("没有设置源路径！");
				return "load_input";
			}
		
			//进入导入程序 
			long end = new Date().getTime();
			//Loggers.info(st.getName()+"导入成功！所用的时间:"+(end-start)/1000+"秒");
			this.setTip("导入成功！所用时间："+(end-start)/1000+"秒");
			return SUCCESS;
		} catch (Exception e) {
			Loggers.error("enterprise模块:ExcelExportAction:economySave()方法出错："+e);
			e.printStackTrace();
		}
		return ERROR;
	}
	
	
	public String corpSave2(){
		try {
			long start = new Date().getTime();
			String source = env.getProperty("corp.import.source.folder");
			if(source != null && source.length() > 0) {
				File folder = new File(source);
				if(folder.isDirectory()) {
					File[] files = folder.listFiles(); 
					for(File xmlfiles:files) {
						if(xmlfiles.getName().endsWith(".xls")) {
							InputStream is = new FileInputStream(xmlfiles.getPath());
							Workbook rwb = Workbook.getWorkbook(is);	//声名一个工作薄
							Sheet st = rwb.getSheet(0);	//第0个工作表
							int Rows=st.getRows();
							int Cols=st.getColumns();
							Loggers.info("Excel数据导入：当前文件名："+xmlfiles.getName()+" 当前工作表的名字："+st.getName()+" 总行数:"+Rows+"总列数:"+Cols);
							
							int count = 0;
							for(int c=0;c<Cols;c++){
								if(null!=st.getCell(c, 0).getContents() && !"".equals(st.getCell(c, 0).getContents())){
									count+=1;
								}
							}
							TbCorpInfo obj = null;
							String county = "";
							String unit = "";
							String dept = "";
							for(int i=1;i<Rows;i++){
								if(null!=st.getCell(0,i).getContents() && st.getCell(0, i).getContents().length()>0){
									String corpName = st.getCell(1, i).getContents();
									System.out.println(corpName);
									if(corpName != null && !"".equals(corpName.trim())) {
										obj = new TbCorpInfo();
										obj.setCorpName(corpName.trim());
										obj.setCorpGsCode(getContent(st, 2, i));
										obj.setCorpOrgCode(getContent(st, 3, i));
										obj.setCorpRegAddress(getContent(st,4,i));
										obj.setCorpWorkAddress(getContent(st,5,i));
										obj.setCorpOfficeAddress(getContent(st,6,i));
										obj.setCorpPost(getContent(st,7,i));
										obj.setCorpPhone(getContent(st,8,i));
										obj.setCorpMobilePhone(getContent(st,9,i));
										obj.setCorpEmail(getContent(st,10,i));
										String type1 = getContent(st,11,i);
										if(null!=type1 && type1.length()>0){
											TbCorpType type = this.getTypeByName(type1.trim());
											if(null!=type){
												obj.setTbCorpType(type);
											}
										}
										String type2 =getContent(st,12,i);
										if(null!=type2 && type2.length()>0){
											TbCorpType type = this.getTypeByName(type2.trim());
											if(null!=type){
												obj.setTbCorpTypeMin(type);
											}
										}
										obj.setCorpProvince(221l);
										obj.setCorpCity(291l);
										county =getContent(st,13,i);
										if(null!=county && county.length()>0){
											TbUserDept d1 = this.getDeptByName(county.trim());
											if(null!=d1){
												obj.setTbCounty(d1);
											}
										}
										unit = getContent(st,14,i);
										if(null!=unit && unit.length()>0){
											TbUserDept d1 = this.getDeptByName(unit.trim());
											if(null!=d1){
												obj.setTbStreet(d1);
											}
										}
										dept = getContent(st,15,i);
										if(null!=dept && dept.length()>0){
											TbUserDept d1 = this.getDeptByName(dept.trim());
											if(null!=d1){
												obj.setTbVillage(d1);
											}
										}
										String zichan = getContent(st,16,i);
										if(zichan != null) {
											zichan = zichan.trim().replaceAll("万", "");
											obj.setCorpGudingzichang(Double.parseDouble(zichan));
										}
										String capital = getContent(st,17,i);
										if(capital != null) {
											capital = capital.trim().replaceAll("万", "");
											//obj.setCorpGudingzichang(Double.parseDouble(capital));
											obj.setCorpZhucezichang(Double.parseDouble(capital));
										}
										String date = getContent(st,18,i);
										if(date != null) {
											obj.setCorpRegDate(formatDate(date));
										}
										//corp area
										
										String area1 =  getContent(st,20,i);
										String area2 =  getContent(st,21,i);
										if(area1 != null) {
											try  {
												obj.setCorpDishang(Double.parseDouble(area1));
											}catch (Exception e) {
												System.out
														.println(e.getMessage());
											}
										}
										if(area2 != null) {
											try  {
												obj.setCorpDishang(Double.parseDouble(area2));
											}catch (Exception e) {
												System.out
														.println(e.getMessage());
											}
										}
										
										
										if(null!=st.getCell(23,i).getContents() && st.getCell(23,i).getContents().length()>0){
											obj.setCorpCyrs(Integer.parseInt(st.getCell(23,i).getContents().trim()));
										}
										if(null!=st.getCell(24,i).getContents() && st.getCell(24,i).getContents().length()>0){
											obj.setCorpAqrs(Integer.parseInt(st.getCell(24,i).getContents().trim()));
										}
										obj.setCorpXkCode(getContent(st, 25, i));
										//安全许可证
										date= getContent(st,26,i);
										if(date != null) {
											obj.setCorpXkStart(formatDate(date));
										}
										date= getContent(st,27,i);
										if(date != null) {
											obj.setCorpXkEnd(formatDate(date));
										}
										//危化许可证
										obj.setCorpWhxkCode(getContent(st, 28, i));
										date= getContent(st,29,i);
										if(date != null) {
											obj.setCorpWhxkStart(formatDate(date));
										}
										date= getContent(st,30,i);
										if(date != null) {
											obj.setCorpWhxkEnd(formatDate(date));
										}
										
										obj.setNationGrade(getContent(st,32,i));
										String reportType = getContent(st, 33, i);
										if(reportType != null) {
											if("半月报".equals(reportType)) {
												obj.setReportType("0");
											} else {
												obj.setReportType("1");
											}
										}
										obj.setCorpJingyixiangmu(getContent(st,34,i));
										obj.setCorpYuanliao(getContent(st,35,i));
										obj.setCorpChanpin(getContent(st,36,i));
									
										String economicType = getContent(st, 37, i);
										if(economicType != null) {
											if("股份有限".equals(economicType)) {
												obj.setCorpProperty("16");
											} else if("国有股份".equals(economicType)) {
												obj.setCorpProperty("11");
											}else if("个人独资".equals(economicType) || "个体".equals(economicType) || 
													"私营独资".equals(economicType) || "个体经营".equals(economicType)) {
												obj.setCorpProperty("17");
											}else if("有限责任公司".equals(economicType)) {
												obj.setCorpProperty("15");
											}else if("集体所有制".equals(economicType)) {
												obj.setCorpProperty("12");
											}else  {
												obj.setCorpProperty("90");
											}
										}
										//System.out.println("is zyb " + getContent(st, 49, i));
										obj.setCorpIsZyb(getContent(st, 47, i));
										obj.setCorpIsTeshe(getContent(st, 48, i));
										obj.setCorpIsShewei(getContent(st, 49, i));
										obj.setCorpIsBlzh(getContent(st, 50, i));
										obj.setIsBlastingDemolition(getContent(st, 51, i));
										obj.setCorpIsDanger(getContent(st, 52, i));
										if("1".equals(getContent(st, 53, i))) {
											obj.setCorpSize("2");
											obj.setCorpSjName(getContent(st, 54, i));
											if(obj.getCorpSjName() != null) {
												TbCorpInfo p = getCorpByName(obj.getCorpSjName());
												if(p != null) {
													obj.setCorpSjId(p.getId());
												}
											}
										} else  {
											obj.setCorpSize("1");
										}
										obj.setCorpStatus(getContent(st, 55, i));
										obj.setDistrictMonitor(getContent(st, 56, i));
										if("1".equals(getContent(st, 57, i))) {
											obj.setCorpLixu("40");
										} else {
											obj.setCorpLixu("50");
										}
									
										obj.setIsDele("0");
										obj.setCorpPwd(PwdDigest.passwordDigest("666666"));
										obj.setCorpPwd2("666666");
										obj.setCorpLoginCount(0);
										obj.setReportType("0");
										obj.setCorpRegType(1l);
										obj.setCorpAddTime(new Date());
										obj.setCorpReportType("1");
										obj.setCorpAddUser("system");
										this.utilService.save(obj);
										
										TbCorpPerson fr = new TbCorpPerson();
										fr.setCorpId(obj.getId());
										fr.setPerName(getContent(st, 38, i));
										fr.setPerPhone(getContent(st, 39, i));//电话
										fr.setPerMobile(getContent(st, 40, i));
										fr.setPerType("1");
										this.utilService.save(fr);
										
										TbCorpPerson fzr = new TbCorpPerson();
										fzr.setCorpId(obj.getId());
										fzr.setPerName(getContent(st, 41, i));
										fzr.setPerPhone(getContent(st, 42, i));//电话
										fzr.setPerMobile(getContent(st, 43, i));
										fzr.setPerType("2");
										this.utilService.save(fzr);
										
										TbCorpPerson aqfzr = new TbCorpPerson();
										aqfzr.setCorpId(obj.getId());
										aqfzr.setPerName(getContent(st, 44, i));
										aqfzr.setPerMobile(getContent(st, 46, i));
										aqfzr.setPerPhone(getContent(st, 45, i));//电话
										aqfzr.setPerType("3");
										this.utilService.save(aqfzr);
										
										//25,26,27安全负责人
										System.out.println("export==="+(i+1)+"行");
									}
								}
								
							}
							rwb.close();
							is.close();
						}
						if(xmlfiles.delete()) {
							System.out.println("Deleted file " + xmlfiles.getName());
						}
					}
				}
				
			} else {
				this.setTip("没有设置源路径！");
				return "load_input";
			}
		
			//进入导入程序 
			long end = new Date().getTime();
			//Loggers.info(st.getName()+"导入成功！所用的时间:"+(end-start)/1000+"秒");
			this.setTip("导入成功！所用时间："+(end-start)/1000+"秒");
			return SUCCESS;
		} catch (Exception e) {
			Loggers.error("enterprise模块:ExcelExportAction:economySave()方法出错："+e);
			e.printStackTrace();
		}
		return ERROR;
	}
	
	public String personSave(){
		try {
			long start = new Date().getTime();
			String source = env.getProperty("person.import.source.folder");
			if(source != null && source.length() > 0) {
				File folder = new File(source);
				if(folder.isDirectory()) {
					File[] files = folder.listFiles(); 
					for(File xmlfiles:files) {
						if(xmlfiles.getName().endsWith(".xls")) {
							InputStream is = new FileInputStream(xmlfiles.getPath());
							Workbook rwb = Workbook.getWorkbook(is);	//声名一个工作薄
							Sheet st = rwb.getSheet(1);	//第0个工作表
							int Rows=st.getRows();
							int Cols=st.getColumns();
							Loggers.info("Excel数据导入：当前文件名："+xmlfiles.getName()+" 当前工作表的名字："+st.getName()+" 总行数:"+Rows+"总列数:"+Cols);
							
							int count = 0;
							for(int c=0;c<Cols;c++){
								if(null!=st.getCell(c, 0).getContents() && !"".equals(st.getCell(c, 0).getContents())){
									count+=1;
								}
							}
							TbUser obj = null;
							String county = "";
							String unit = "";
							String dept = "";
							for(int i=2;i<Rows;i++){
								if(null!=st.getCell(0,i).getContents() && st.getCell(0, i).getContents().length()>0){
									String userName = st.getCell(0, i).getContents();
									System.out.println(userName);
									if(userName != null && !"".equals(userName.trim())) {
										obj = new TbUser();
										obj.setUserName(userName);
										obj.setUserRealName(userName);
										
										String grade =getContent(st,2,i);
										if(null!=grade && grade.length()>0){
											TbUserGrade ugrade = this.getGradeByName(grade);
											if(null!=ugrade){
												obj.setTbUserGrade(ugrade);
											}
										}
										String sex = getContent(st,3,i);
										if("男".equals(sex)) {
											obj.setUserSex("1");
										} else {
											obj.setUserSex("0");
										}
										String uunit = getContent(st,4,i);
										TbUserDept u = null;
										if(uunit != null) {
											u = this.getUnitByName(uunit, null);
											if(u != null) {
												obj.setTbUserUnit(u);
											}
										}
										String udept = getContent(st,5,i);
										TbUserDept d = null;
										if(uunit != null) {
											Long parentid = null;
											if(u != null) {
												parentid = u.getId();
											}
											d = this.getUnitByName(udept, parentid);
											if(u != null) {
												obj.setTbUserDept(d);
											}
										}
										String zw = getContent(st, 6, i);
										if(zw != null) {
											if("科长".equals(zw)) {
												obj.setUserZw("5");
											} else if("科员".equals(zw)) {
												obj.setUserZw("9");
											}else if("股长".equals(zw)) {
												obj.setUserZw("10");
											}else if("副股长".equals(zw)) {
												obj.setUserZw("11");
											}else if("股员".equals(zw)) {
												obj.setUserZw("12");
											}
										}
										String roleName = getContent(st, 7, i);
										if(roleName != null) {
											TbUserRole role = this.getRoleByName(roleName);
											if(role!=null) {
												obj.setTbUserRole(role);
											}
										}
										obj.setUserPhone( getContent(st, 8, i));
										obj.setUserEmail( getContent(st, 9, i));
										obj.setIsLock("0");
										obj.setIsDele("0");
										obj.setUserLoginCount(0);
										obj.setUserAddTime(new Date());
										obj.setUserPwd(PwdDigest.passwordDigest(Constants.USER_PASSWORD));
										this.utilService.save(obj);
										//10,11,12 法人
										//22,23,24主要负责人
										
										//25,26,27安全负责人
										System.out.println("saved person ==="+ userName+"行");
									}
								}
								
							}
							rwb.close();
							is.close();
						}
						if(xmlfiles.delete()) {
							System.out.println("Deleted file " + xmlfiles.getName());
						}
					}
				}
				
			} else {
				this.setTip("没有设置源路径！");
				return "load_input";
			}
		
			//进入导入程序 
			long end = new Date().getTime();
			//Loggers.info(st.getName()+"导入成功！所用的时间:"+(end-start)/1000+"秒");
			this.setTip("导入成功！所用时间："+(end-start)/1000+"秒");
			return SUCCESS;
		} catch (Exception e) {
			Loggers.error("enterprise模块:ExcelExportAction:economySave()方法出错："+e);
			e.printStackTrace();
		}
		return ERROR;
	}
	
	private Date formatDate(String dstr) {
		String[] pattern = {"yyyy/MM/dd","yyyy.MM.dd","yyyy/M/d","yyyy/MM/d", "yyyy/M/dd","yyyy-MM-dd", "yyyy-M-d",
				"yyyy-MM.dd","yyyy-MM--dd","yyyy-MM-d", "yyyy-M-dd", "yyyy-M", "yyyy-MM"}	;
		Date date = null;
		String format = "";
		for(int i = 0; i < pattern.length; i ++) {
			format = pattern[i];
			try {
	    		SimpleDateFormat sdf = new SimpleDateFormat(format);
	    		date = sdf.parse(dstr);
	    		break;
	          } catch (Exception e) {
	        	  continue;
	          }
		}
	
		 System.out.println(format + "===== " + dstr);      
		 System.out.println("Converted date is " + date);
		    return date;
	}
	/**
	 * 导入企业
	 * @return
	 */
	public String updateSave(){
		try {
			if(this.dbFile == null || !this.dbFile.exists()){
				this.setTip("附件不能为空！");
				return "load_input";
			}
			//进入导入程序 
			long start = new Date().getTime();
			File file = this.dbFile;
			InputStream is = new FileInputStream(file.getPath());
			Workbook rwb = Workbook.getWorkbook(is);	//声名一个工作薄
			Sheet st = rwb.getSheet(0);	//第0个工作表
			int Rows=st.getRows();
			int Cols=st.getColumns();
			Loggers.info("Excel数据导入：当前文件名："+this.dbFileFileName+" 当前工作表的名字："+st.getName()+" 总行数:"+Rows+"总列数:"+Cols);
			
			int count = 0;
			for(int c=0;c<Cols;c++){
				if(null!=st.getCell(c, 0).getContents() && !"".equals(st.getCell(c, 0).getContents())){
					count+=1;
				}
			}
			TbCorpInfo obj = null;
			for(int i=0;i<Rows;i++){
				if(null!=st.getCell(1,i).getContents() && st.getCell(1, i).getContents().length()>0){
					obj = this.getCorpByName(st.getCell(1,i).getContents().trim());
					if(null!=obj){
						TbUserDept d = this.getDeptByCode(st.getCell(2,i).getContents().trim());
						if(null!=d){
							TbUserDept d1 = (TbUserDept)this.utilService.getObjectById(TbUserDept.class, d.getDeptParentId());
							if(null!=d1){
								obj.setTbStreet(d1);
							}
							obj.setTbVillage(d);
							obj.setCorpOrgCode(st.getCell(0,i).getContents().trim());
							this.utilService.update(obj);
						}
					}
				}
				System.out.println("export==="+(i+1)+"行");
			}
			rwb.close();
			long end = new Date().getTime();
			Loggers.info(st.getName()+"导入成功！所用的时间:"+(end-start)/1000+"秒");
			this.setTip("导入成功！所用时间："+(end-start)/1000+"秒");
			return SUCCESS;
		} catch (Exception e) {
			Loggers.error("enterprise模块:ExcelExportAction:economySave()方法出错："+e);
			e.printStackTrace();
		}
		return ERROR;
	}
	
	public TbUserDept getDeptByName(String str){
		TbUserDept dept = null;
		try {
			List list = (List)this.utilService.getHqlQuery("from TbUserDept t where 1=1 and t.deptName='"+str+"'");
			if(null!=list && list.size()>0){
				return (TbUserDept)list.get(0);
			}
		} catch (Exception e) {
			Loggers.error("enterprise模块:TbUserDept getDeptByName(String str)方法出错："+e);
			e.printStackTrace();
		}
		return dept;
	}
	
	public TbUserDept getDeptByCode(String str){
		TbUserDept dept = null;
		try {
			List list = (List)this.utilService.getHqlQuery("from TbUserDept t where 1=1 and t.deptCode='"+str+"'");
			if(null!=list && list.size()>0){
				return (TbUserDept)list.get(0);
			}
		} catch (Exception e) {
			Loggers.error("enterprise模块:TbUserDept getDeptByCode(String str)方法出错："+e);
			e.printStackTrace();
		}
		return dept;
	}
	
	public TbCorpType getTypeByName(String str){
		TbCorpType type = null;
		try {
			List list = (List)this.utilService.getHqlQuery("from TbCorpType t where 1=1 and t.typeName='"+str+"'");
			if(null!=list && list.size()>0){
				return (TbCorpType)list.get(0);
			}
		} catch (Exception e) {
			Loggers.error("enterprise模块:TbCorpType getTypeByName(String str)方法出错："+e);
			e.printStackTrace();
		}
		return type;
	}
	
	public TbUserGrade getGradeByName(String str){
		TbUserGrade grade = null;
		try {
			List list = (List)this.utilService.getHqlQuery("from TbUserGrade t where 1=1 and t.gradeName='"+str+"'");
			if(null!=list && list.size()>0){
				return (TbUserGrade)list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return grade;
	}
	
	public TbUserDept getUnitByName(String str,Long parentid){
		TbUserDept grade = null;
		try {
			String sql = "from TbUserDept t where 1=1 and t.deptName='"+str+"'";
			if(parentid != null) {
				sql += " and t.deptParentId = " + parentid;
			}
			List list = (List)this.utilService.getHqlQuery(sql);
			if(null!=list && list.size()>0){
				return (TbUserDept)list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return grade;
	}
	
	public TbCorpInfo getCorpByName(String str){
		TbCorpInfo corp = null;
		try {
			List list = (List)this.utilService.getHqlQuery("from TbCorpInfo t where 1=1 and t.corpName='"+str+"'");
			if(null!=list && list.size()>0){
				return (TbCorpInfo)list.get(0);
			}
		} catch (Exception e) {
			Loggers.error("enterprise模块:TbCorpType getTypeByName(String str)方法出错："+e);
			e.printStackTrace();
		}
		return corp;
	}
	
	public TbUserRole getRoleByName(String str){
		TbUserRole corp = null;
		try {
			List list = (List)this.utilService.getHqlQuery("from TbUserRole t where 1=1 and t.roleName='"+str+"'");
			if(null!=list && list.size()>0){
				return (TbUserRole)list.get(0);
			}
		} catch (Exception e) {
			Loggers.error("enterprise模块:TbCorpType getTypeByName(String str)方法出错："+e);
			e.printStackTrace();
		}
		return corp;
	}

	public File getDbFile() {
		return dbFile;
	}

	public void setDbFile(File dbFile) {
		this.dbFile = dbFile;
	}

	public String getDbFileContentType() {
		return dbFileContentType;
	}

	public void setDbFileContentType(String dbFileContentType) {
		this.dbFileContentType = dbFileContentType;
	}

	public String getDbFileFileName() {
		return dbFileFileName;
	}

	public void setDbFileFileName(String dbFileFileName) {
		this.dbFileFileName = dbFileFileName;
	}

}
